package cs3343.project.family;

import javax.swing.*;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class InitGame {
	static JFrame gameFrame = new JFrame("Family - Game");
	static JPanel gamePanel = new JPanel();
	static JPanel playerPanel = new JPanel();
	static JPanel debugPanel = new JPanel();
	static JPanel esPanel = new JPanel();
	
	//TODO:
	static JLabel curPlay;
	static JLabel curSE;
	static JLabel curIndex;
	static JLabel diceNum;
	static JComboBox drawCard;
	static JButton button;
	/*
	static JLabel img1;
	static JLabel img2;
	static JLabel img3;
	static JLabel icon1;
	static JLabel icon2;
	static JLabel icon3;
	static JLabel name1;
	static JLabel name2;
	static JLabel name3;
	static JLabel es1;
	static JLabel es2;
	static JLabel es3;*/
	static JLabel[] img = new JLabel[6];
	static JLabel[] icon = new JLabel[6];
	static JLabel[] name = new JLabel[6];
	static JLabel[] se = new JLabel[6];
	
	static Container contentPane;
	
	static MapArea[] map = new MapArea[40];

	static int curPlayer = 0;
	static boolean firstInit = false;
	
	public InitGame() {
		
		//int playerNum = ProfileManager.getProfileSize();
				
		CardController.importAllCard();
		contentPane = gameFrame.getContentPane();
		gamePanel.setBackground(Color.LIGHT_GRAY);
		playerPanel.setBackground(Color.DARK_GRAY);
		esPanel.setBackground(Color.DARK_GRAY);
		debugPanel.setBackground(Color.WHITE);
	
		gameFrame.add(playerPanel, BorderLayout.EAST);
	    gameFrame.add(gamePanel, BorderLayout.WEST);
	    gameFrame.add(debugPanel, BorderLayout.SOUTH);
	    
	    //create dice
	    ImageIcon dice = new ImageIcon(ConfigManager.IMAGE_PATH + "dice.jpg");
	    button = new JButton("Roll", dice);
	    Dice.createDice();
	    

	    
	    //TODO: hard code create char 
	    //create char
	    /*
	    //Start.character[1] = new Character("SorJai1", 'M', 0, new ImageIcon("src/profilePic1.jpg"), new ImageIcon("src/char1.jpg"), 100);
	    img1 = new JLabel(StartGame.character[1].getImage());
	    icon1 = new JLabel(StartGame.character[1].getIcon());
	    name1 = new JLabel(StartGame.character[1].getName());
	    es1 = new JLabel(String.valueOf(StartGame.character[1].getEsteem()));
	    
	    //Start.character[2] = new Character("SorJai2", 'M', 0, new ImageIcon("src/profilePic2.jpg"), new ImageIcon("src/char2.jpg"), 100);
	    img2 = new JLabel(StartGame.character[2].getImage());
	    icon2 = new JLabel(StartGame.character[2].getIcon());
	    name2 = new JLabel(StartGame.character[2].getName());
	    es2 = new JLabel(String.valueOf(StartGame.character[2].getEsteem()));
	    
	    //Start.character[3] = new Character("SorJai3", 'M', 0, new ImageIcon("src/profilePic3.jpg"), new ImageIcon("src/char3.jpg"), 100);
	    img3 = new JLabel(StartGame.character[3].getImage());
	    icon3 = new JLabel(StartGame.character[3].getIcon());
	    name3 = new JLabel(StartGame.character[3].getName());
	    es3 = new JLabel(String.valueOf(StartGame.character[3].getEsteem()));
	    */
	    

	    
	    for(int i = 0; i < StartGame.selectedPlayer; i++ ){
		    img[i] = new JLabel(StartGame.character[i].getImage());
		    icon[i] = new JLabel(StartGame.character[i].getIcon());
		    name[i] = new JLabel(StartGame.character[i].getName());
		    se[i] = new JLabel(String.valueOf(StartGame.character[i].getEsteem()));
		    playerPanel.add(img[i]);
		    
		    if(i == 0)
		    	img[i].setVisible(true);
		    else
		    	img[i].setVisible(false);
	    }
	    
	    for(int i = StartGame.selectedPlayer; i < ProfileManager.getProfileSize()-1; i++)
	    	playerPanel.add(new JLabel(" "));
	    	
	    esPanel.setLayout(new GridLayout(2, 3));
	    
	    playerPanel.setLayout(new GridLayout(7, 1));
	    
	    

	    playerPanel.add(button);
	    /*img1.setVisible(true);
	    img2.setVisible(false);
	    img3.setVisible(false);
	    
	    /*esPanel.add(name1);
	    esPanel.add(es1);
	    esPanel.add(name2);
	    esPanel.add(es2);
	    esPanel.add(name3);
	    esPanel.add(es3);
	    esPanel.setBounds(gamePanel.getWidth()/2,gamePanel.getHeight()/2, esPanel.getWidth(), esPanel.getHeight());
	    gamePanel.add(esPanel);*/
	    
	    for(int i = 0; i < StartGame.selectedPlayer; i++){
	    	name[i].setBounds(200, 160+20*i, 200, 200);
	    	se[i].setBounds(250, 160+20*i, 200, 200);
	    	icon[i].setBounds(0, 0, 100, 100);
	    	gamePanel.add(name[i]); 
	    	gamePanel.add(se[i]); 
	    	gamePanel.add(icon[i]);
	    }
	    
	    //char on map
	    /*icon1.setBounds(0, 0, 100, 100);
	    icon2.setBounds(0, 0, 100, 100);
	    icon3.setBounds(0, 0, 100, 100);
	    gamePanel.add(icon1); 
	    gamePanel.add(icon2); 
	    gamePanel.add(icon3);*/ 
    
	    new MapController(StartGame.selectedPlayer);

	    gamePanel.setLayout(null);  
    
    
	    //for debug
	    debugPanel.setLayout(new GridLayout(1,5));
	  	curPlay = new JLabel(" ");
	  	curSE = new JLabel(" ");
		curIndex = new JLabel(" ");
		diceNum = new JLabel(" ");
		drawCard = new JComboBox();	
		drawCard.addItem("No Card");
		debugPanel.add(curPlay);
		debugPanel.add(curSE);
		debugPanel.add(curIndex);
		debugPanel.add(diceNum);
		debugPanel.add(drawCard);
	    Font f = new Font("Dialog", Font.PLAIN, 24);
	    curPlay.setFont(f);
	    curSE.setFont(f);
	    curIndex.setFont(f);
	    diceNum.setFont(f);
    
	    //TODO: wrong sequence map
	    /*//draw map
	    for(int i = 0, k = 0; i < 5; i++)
	    	for(int j = 0; j < 5; j++){
	    		if((i == 0 || i == 4 || j == 0 || j == 4)){
	    			
	    			mapImg[k].setBounds(i*map[k].getImage().getIconWidth(),
	    								j*map[k].getImage().getIconHeight(),
	    								map[k].getImage().getIconWidth(),
	    								map[k].getImage().getIconHeight() );
	    			
	    			map[k].setX(i*map[k].getImage().getIconWidth());
	    			map[k].setY(j*map[k].getImage().getIconHeight());
					
	    			gamePanel.add(mapImg[k]);
	    			k++;
	    		}
	    		else
	    			gamePanel.add(new JLabel(" "));
	    	}
	    */

	    //TODO throw the following to control class
	    //button.setHorizontalTextPosition(SwingConstants.LEFT);
	    
		//dice pressed
		
		//add to panel
	    contentPane.add(gamePanel);
	    gameFrame.setVisible(true);
	  	gameFrame.addWindowListener(new WindowAdapter() {
	  		public void windowClosing(WindowEvent e) {
	  			System.exit(0);
	  		}
	  	});
	  	
	  	gameFrame.show();
	 	  	
		button.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent e) {
				  DiceController gc = new DiceController();
				  gc.buttonPress();
			  }
		});

	}
	
	public JFrame getGameFrame(){
		return this.gameFrame;
		
	}
}
